﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp_GetSystemMemoryAndCPUInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            //调用GetSystemInfo函数获取CPU的相关信息
            CPU_INFO CpuInfo;
            CpuInfo = new CPU_INFO();
            GetSystemInfo(ref CpuInfo);

            Console.WriteLine("本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU");
            Console.WriteLine("CPU的类型为" + CpuInfo.dwProcessorType.ToString());
            Console.WriteLine("CPU等级为" + CpuInfo.dwProcessorLevel.ToString());
            Console.WriteLine("CPU的OEM ID为" + CpuInfo.dwOemId.ToString());
            Console.WriteLine("CPU中的页面大小为" + CpuInfo.dwPageSize.ToString());
            Console.WriteLine("" + CpuInfo.dwActiveProcessorMask.ToString());
            Console.WriteLine("" + CpuInfo.dwAllocationGranularity.ToString());
            Console.WriteLine("" + CpuInfo.dwProcessorRevision.ToString());
            Console.WriteLine("" + CpuInfo.lpMaximumApplicationAddress.ToString());
            Console.WriteLine("" + CpuInfo.lpMinimumApplicationAddress.ToString());

            //调用GlobalMemoryStatus函数获取内存的相关信息
            MEMORY_INFO MemInfo;
            MemInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref MemInfo);

            //感觉的这个使用率有点假
            Console.WriteLine(MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用");
            Console.WriteLine("物理内存共有" + ((MemInfo.dwTotalPhys / 1024) / 1024).ToString() + "MB");
            Console.WriteLine("可使用的物理内存有" + ((MemInfo.dwAvailPhys / 1024) / 1024).ToString() + "MB");
            Console.WriteLine("交换文件总大小为" + ((MemInfo.dwTotalPageFile / 1024) / 1024).ToString() + "MB");
            Console.WriteLine("尚可交换文件大小为" + ((MemInfo.dwAvailPageFile / 1024) / 1024).ToString() + "MB");
            Console.WriteLine("总虚拟内存有" + ((MemInfo.dwTotalVirtual / 1024) / 1024).ToString() + "MB");
            Console.WriteLine("未用虚拟内存有" + ((MemInfo.dwAvailVirtual / 1024) / 1024).ToString() + "MB");

            Console.Read();
        }

        [System.Runtime.InteropServices.DllImport("kernel32")]
        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);

        [System.Runtime.InteropServices.DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
    }

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct CPU_INFO
    {
        /// <summary>
        /// CPU的OEM_ID
        /// </summary>
        public uint dwOemId;

        /// <summary>
        /// CPU中的页面大小
        /// </summary>
        public uint dwPageSize;

        /// <summary>
        /// 最小地址
        /// </summary>
        public uint lpMinimumApplicationAddress;

        /// <summary>
        /// 最大地址
        /// </summary>
        public uint lpMaximumApplicationAddress;

        public uint dwActiveProcessorMask;

        /// <summary>
        /// CPU个数
        /// </summary>
        public uint dwNumberOfProcessors;

        /// <summary>
        /// CPU的类型
        /// </summary>
        public uint dwProcessorType;

        public uint dwAllocationGranularity;

        /// <summary>
        /// CPU等级
        /// </summary>
        public uint dwProcessorLevel;

        public uint dwProcessorRevision;

    }

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct MEMORY_INFO
    {
        public uint dwLength;

        /// <summary>
        /// 被使用内存(百分率)
        /// </summary>
        public uint dwMemoryLoad;

        /// <summary>
        /// 物理内存(字节)
        /// </summary>
        public uint dwTotalPhys;

        /// <summary>
        /// 可使用的物理内存(字节)
        /// </summary>
        public uint dwAvailPhys;

        /// <summary>
        /// 交换文件总大小(字节)
        /// </summary>
        public uint dwTotalPageFile;

        /// <summary>
        /// 尚可交换文件大小(字节)
        /// </summary>
        public uint dwAvailPageFile;

        /// <summary>
        /// 总虚拟内存(字节)
        /// </summary>
        public uint dwTotalVirtual;

        /// <summary>
        /// 未用虚拟内存(字节)
        /// </summary>
        public uint dwAvailVirtual;


    }
}
